package logic

import (
	"context"
	"fmt"
	"medical-management-system/models/mysql"

	"medical-management-system/medicalrpc/internal/svc"
	"medical-management-system/medicalrpc/medicalrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RegisterLogic) Register(in *medicalrpc.RegisterRequest) (*medicalrpc.RegisterResponse, error) {
	// todo: add your logic here and delete this line
	var medical mysql.Medical
	mysql.DB.Debug().Find(&medical)
	if medical.ID != 0 {
		return nil, fmt.Errorf("账号已被注册，请前去登录")
	}

	medical = mysql.Medical{
		Account:  in.Account,
		Password: in.Password,
	}
	err := mysql.DB.Debug().Create(&medical).Error
	if err != nil {
		return nil, fmt.Errorf("注册失败")
	}
	return &medicalrpc.RegisterResponse{Id: int64(medical.ID)}, nil
}
